#ifndef _H_X_LPA
#define _H_X_LPA

#include <vector>
#include <QWidget>
#include <QProgressBar>
#include <QLabel>
#include <QGridLayout>
#include <fstream>
#include <sndfile.hh>
#include "Basic.h"


typedef double XSReal;


// Signal to Noise Ratio
class SNR {
	double s;
	int n;
public:
	SNR();
	void add(double x, double y);
	void merge(const SNR &snr);
	double value();
};


struct XPara {
	int channels;
	int samplerate;
	sf_count_t frames;
	int window;
	int hlen;
};


class XSegment {
	double **theta;
	double *t0;
	double *t, *r;
	XSReal **a, **c;
	short *__buf;
	int channels, n, hlen;

	double bestDelta;
	int period;
	int bestInd;
	double bestHeight;

	void findPeriod();
	double bestScale();
	void updateStart(int p, int ind);
public:
	XSegment(int channels, int n, int hlen);
	~XSegment();
	void print();
	void load(std::ifstream &file);
	void save(std::ofstream &file);
	void make(double samples[]);
	void get(double samples[]);
};


class XLpa : public QWidget {
	Q_OBJECT

	QString inputFile;
	QProgressBar *progressBar;
	QLabel *label[5];
public:
	XLpa(QWidget *parent = 0);
	void convert(QString outputFile);
	void update(QString inputFile);
};

#endif
